package dto

import (
	"time"

	"gorm.io/gorm"
)

type ReqIdDto struct {
	Id int `form:"id" json:"id" binding:"required" required_msg:"id 为必填项目"`
}
type ReqUriIdDto struct {
	Id int `uri:"id" binding:"required" required_msg:"id 为必填项目" validate:"required"`
}
type ReqId struct {
	Id int `json:"id" binding:"required" required_msg:"id 为必填项目"`
}

type ReqUriTime struct {
	StartDate time.Time `uri:"time" time_format:"2006-01-02" binding:"required" required_msg:"time 为必填项目" validate:"required"`
}

type ReqUriRang struct {
	Rang int `form:"rang" json:"rang"  binding:"omitempty,max=12" required_msg:"rang 为搜索范围" max_msg:"rang max=12为搜索范围"`
}

/** key这个属性默认值为"user" 非use搜索一定要加 key这个属性*/
type ReqPaginat struct {
	Page     int    `form:"page" json:"page" binding:"required" required_msg:"page 为必填项目"`
	Limit    int    `form:"limit" json:"limit" binding:"required" required_msg:"limite 为必填项目"`
	Keyword  string `form:"keyword" json:"keyword" binding:"omitempty" required_msg:"keyword 为必填项目,搜索的关键词"`
	Order    string `form:"order" json:"order"  binding:"omitempty,oneof=asc desc" oneof_msg:"order 为必填项目 asc | desc"`
	Key      string `form:"key" json:"key"`
	OrderKey string `form:"orderkey" json:"orderkey"`
}

// TODo 添加动态修改struct上的tag
type ReqPaginatPm struct {
	Page     int    `form:"page" json:"page" binding:"required" required_msg:"page 为必填项目"`
	Limit    int    `form:"limit" json:"limit" binding:"required" required_msg:"limite 为必填项目"`
	Keyword  string `form:"keyword" json:"keyword" binding:"omitempty" required_msg:"keyword 为必填项目,搜索的关键词"`
	Key      string `form:"key" json:"key" binding:"omitempty,oneof=id created_at updatad_at fullpath url method desc" oneof_msg:"key 为选填项目,key搜索的属性名必须为fullpath url method desc id created_at updatad_at"`
	Order    string `form:"order" json:"order" binding:"omitempty,oneof=asc desc" oneof_msg:"order 为必填项目 asc | desc"`
	OrderKey string `form:"orderkey" json:"orderkey" binding:"omitempty,oneof=id created_at updatad_at fullpath url method desc" oneof_msg:"orderkey 为选填项目,key搜索的属性名必须为fullpath url method desc id created_at updatad_at"`
}

func (m *ReqPaginat) PaginatFn(orm *gorm.DB) func(orm *gorm.DB) *gorm.DB {
	//BUG 搜索时 total并没有按搜索数据
	if m.OrderKey == "" {
		m.OrderKey = "id"
	}
	return func(orm *gorm.DB) *gorm.DB {
		limit := m.FilterRepLimit(m.Limit)
		var keyword string
		if m.Keyword != "" {
			keyword = "%" + m.Keyword + "%"
			return orm.Where(m.Key+" LIKE ?", keyword).Offset((m.FilterRepPage(m.Page) - 1) * limit).Limit(limit).Order(m.OrderKey + " " + m.Order)
		} else {
			return orm.Offset((m.FilterRepPage(m.Page) - 1) * limit).Limit(limit).Order(m.OrderKey + " " + m.Order)
		}
	}
}

// 使用map模拟结构体标签的动态修改
func (m *ReqPaginat) FilterRepPage(page int) int {
	if m.Page < 1 {
		m.Page = 1
	}
	return m.Page
}
func (m *ReqPaginat) FilterRepLimit(limit int) int {
	if m.Limit < 1 {
		m.Limit = 10
	}
	return m.Limit
}

// TransCommandPagi 函数，用于将obj的属性复制到ReqPaginat实例
func (m *ReqPaginat) TransCommandPagi(obj interface{}) *ReqPaginat {
	// 类型断言确保obj可以转换为ReqPaginat类型
	if paginatedObj, ok := obj.(ReqPaginat); ok {
		// 创建一个新的ReqPaginat实例，并复制属性
		newObj := ReqPaginat{
			Page:     paginatedObj.Page,
			Limit:    paginatedObj.Limit,
			Keyword:  paginatedObj.Keyword,
			Key:      paginatedObj.Key,
			Order:    paginatedObj.Order,
			OrderKey: paginatedObj.OrderKey,
		}
		return &newObj
	}
	// 如果类型断言失败，返回nil或者可以根据需要处理错误
	return nil
}
